#!/usr/bin/env python
# file name check_pmc_order.py
__author__ = 'Perry'
from lib.format_time import *
from lib.HandleMysql import *
from lib.handle_mongo import *
from lib.switch_code import *
from lib.load_data import *
from zhineng_conn import *
from create_table import *
from config_tools import *
import urllib
import sys
import re
import time
reload(sys)
sys.setdefaultencoding('utf-8')




def get_order_info( date ,conn ,num):
    stat_conn = set_stats_conn()
    #stat_table = "ad_order_info"
    line_list = []
    total_amount = 0.0
    total_discount_amount = 0.0
    total_refund_cash_amount = 0.0
    total_refund_discount_cash_amount = 0.0
    query = "select id,order_id from orders where mer_id=7202 and date(finish_time)='%s' " %date
    items = conn.get_results(query)
    for item in items:
        order_code = item[1]
        if order_code == None:
            continue
        order_id = item[0]
        cash_amount = 0.0
        discount_amount = 0.0
        refund_cash_amount = 0.0
        refund_discount_amount = 0.0
        q = "select order_money,from_account_type,ifnull(refund_money,0) from sub_order where order_id='%s'"%order_id
        its = conn.get_results(q)
        if len(its)>0:
            for it in its:
                order_amount = it[0]
                amount_type = it[1]
                refund_money = it[2]
                if amount_type == 3:
                    discount_amount += float(order_amount)
                    refund_discount_amount += float(refund_money)
                else:
                    cash_amount += float(order_amount)
                    refund_cash_amount += float(refund_money)
            total_amount += cash_amount
            total_discount_amount += discount_amount
            total_refund_cash_amount += refund_cash_amount
            total_refund_discount_cash_amount += refund_discount_amount
            param = "No. %s database . Order ID: %s .: total_amount: %s; total_discount_amount: %s; total_refund_cash_amount: %s; total_refund_discount_cash_amount: %s;\n" \
                    %(num, order_id, total_amount, total_discount_amount, total_refund_cash_amount, total_refund_discount_cash_amount)
            print param
            params = [order_code, order_id, num, cash_amount, discount_amount, refund_cash_amount, refund_discount_amount, date]
            line_list.append('\t'.join(map(lambda i : str(i), params)))

    cont = '\n'.join(line_list)
    path = "/work3/opt/analytic/lh_tmp_data/zhineng/check_file/order_file/"
    file_name = "zhineng_pmc_order_info_%s_%s.txt"%(date.replace('-',""), num)
    writeTextToFile(path,file_name,"w",cont)

    ## write result
    param = "No. %s database : total_amount: %s; total_discount_amount: %s; total_refund_cash_amount: %s; total_refund_discount_cash_amount: %s;\n" %(num, total_amount, total_discount_amount, total_refund_cash_amount, total_refund_discount_cash_amount)

    path = "/work3/opt/analytic/lh_tmp_data/zhineng/check_file/"
    file_name = "zhineng_order_info_%s_%s.txt" % (date.replace('-',""), num)
    writeTextToFile(path, file_name, "w", param)
    conn.close()
    stat_conn.close()



def get_all_order_info(date):
    print "getting orders"
    try:
        pass
        order_conn = set_order0_conn()
        get_order_info(date,order_conn,0)
        order_conn = set_order1_conn()
        get_order_info(date,order_conn,1)
        order_conn = set_order2_conn()
        get_order_info(date,order_conn,2)
        order_conn = set_order3_conn()
        get_order_info(date,order_conn,3)
    except Exception,e:
        print 'getting order info error:',e



def main():
    date = ""
    if len(sys.argv) >= 2:
        date = format_date(sys.argv[1])
    else:
        date = get_yesterday()

    get_all_order_info(date)


if __name__=='__main__':
    main()


